
package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.LineCreateDto;
import com.example.front.dto.LineDto;
import com.example.front.dto.LineSearchDto;
import com.example.front.entity.Line;
import com.example.front.serivce.LineService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "产线管理")
@RestController
@CrossOrigin
@RequestMapping("/front/line")
public class LineController {

    @Resource
    LineService lineService;

    @ApiOperation("添加产线")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid LineCreateDto lineCreateDto) {
        User user = UserContext.getUser();
        Line line = new Line();
        BeanUtils.copyProperties(lineCreateDto, line);
        line.setTenantId(user.getTenantId());
        lineService.createLine(line);
        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("删除产线")
    @PostMapping("/delete")
    public JsonResult<String> delete(Long id){
        User user = UserContext.getUser();
        lineService.deleteLine(user.getTenantId(),id);
        return new JsonResult<>(true, "删除成功");
    }

    @ApiOperation("修改数据")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid LineDto lineDto){
        User user = UserContext.getUser();
        Line line = new Line();
        BeanUtils.copyProperties(lineDto, line);
        line.setTenantId(user.getTenantId());
        lineService.updateLine(line);
        return new JsonResult<>(true, "修改成功");
    }

    @ApiOperation("产线列表")
    @GetMapping("/list")
    public JsonResult<ListDto<LineDto>> list(LineSearchDto condition, Pagination pagination){
        User user = UserContext.getUser();
        Line line = new Line();
        BeanUtils.copyProperties(condition, line);
        line.setTenantId(user.getTenantId());
        return new JsonResult<>(lineService.listLine(line,pagination));
    }

}

